package com.example.myviewgroup;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * author: ts
 * created on: 2021/8/27 下午4:38
 * description:
 */
public class MyViewGroupAdapter extends MyBaseAdapter {

    public List<String> mList;

    private final Context mContext;

    public boolean mIsEdit;


    private static final String TAG = "MyAdapter";

    public MyViewGroupAdapter(Context context) {
        mContext = context;
    }

    public void setData(List<String> list){
        mList = list;
        notifyDataChanged();
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public View getView(int position, ViewGroup viewGroup) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_history_view, viewGroup, false);
        TextView textView = view.findViewById(R.id.tv_content);
        textView.setText(mList.get(position));
        ImageView imageView = view.findViewById(R.id.image_view);
        if (mIsEdit){
            imageView.setVisibility(View.VISIBLE);
            imageView.setOnClickListener(v->{
                if (onDeleteItemClickListener != null) {
                    onDeleteItemClickListener.onDeleteItemClick(position);
                }
                Log.d(TAG, "getView: onDeleteItemClickListener is null");
            });
        } else {
            imageView.setVisibility(View.GONE);
        }
        return view;
    }

    private OnDeleteItemClickListener onDeleteItemClickListener;

    public void setOnDeleteItemClickListener(OnDeleteItemClickListener onRecyclerItemClickListener) {
        this.onDeleteItemClickListener = onRecyclerItemClickListener;
    }

    public interface OnDeleteItemClickListener {
        void onDeleteItemClick(int position);
    }

}
